package com.ep.framelibrary.http;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.ep.baselibrary.http.EngineCallBack;
import com.ep.baselibrary.http.HttpUtils;
import com.ep.baselibrary.http.IHttpEngine;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/4.
 */

public class OkhttpEngine implements IHttpEngine {

    private static OkHttpClient mOkHttpClient=new OkHttpClient();
    private static final MediaType mMediaType=MediaType.parse("text/xml; charset=utf-8");//看接口文档做相应修改
    private static Handler mHandler=new Handler();
    @Override
    public void get(boolean cache, Context context, String url, Map<String,Object>map, EngineCallBack callBack) {
        String finalUrl=url+ HttpUtils.jointParams(url,map);
        //是否缓存，是，则查询是否有缓存，有则返回数据
        if (cache){
            String cacheResult = CacheDataUtil.getCacheResult(finalUrl);
            if (!TextUtils.isEmpty(cacheResult)){
                callBack.onSuccess(cacheResult);
            }
        }

        Request request=new Request.Builder().get().url(finalUrl).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mHandler.post(() -> callBack.onError(e));

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                //比对缓存数据，若一致则界面不做更新，反之做相应修改
                if (cache){
                    String cacheResult = CacheDataUtil.getCacheResult(finalUrl);
                    if (!TextUtils.isEmpty(cacheResult)&&!TextUtils.isEmpty(result)&&cacheResult.equals(result)){
                        return;
                    }
                }
                mHandler.post(() -> callBack.onSuccess(result));

                //要缓存则缓存最新数据
                if (cache){
                    CacheDataUtil.cacheData(finalUrl,result);
                }
            }
        });
    }

    @Override
    public void post(boolean cache, Context context, String url, String body, EngineCallBack callBack) {

        String finalUrl=url+body;
        //是否缓存，是，则查询是否有缓存，有则返回数据
        if (cache){
            String cacheResult = CacheDataUtil.getCacheResult(finalUrl);
            if (!TextUtils.isEmpty(cacheResult)){
                callBack.onSuccess(cacheResult);
            }
        }

        //请求数据
        RequestBody requestBody=RequestBody.create(mMediaType,body);
        Request request=new Request.Builder().url(url).post(requestBody).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mHandler.post(() -> callBack.onError(e));
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //比对缓存数据，若一致则界面不做更新，反之做相应修改
                String result = response.body().string();
                if (cache){
                    String cacheResult = CacheDataUtil.getCacheResult(finalUrl);
                    if (!TextUtils.isEmpty(cacheResult)&&cacheResult.equals(result)){
                        return;
                    }
                }

                mHandler.post(() -> callBack.onSuccess(result));

                //要缓存则缓存最新数据
                if (cache){
                    CacheDataUtil.cacheData(finalUrl,result);
                }
            }
        });

    }
}
